package com.gy.componit;

import com.gy.game.Game;
import com.gy.utils.CrashUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * @author Dsy
 * @create 2022-02-03  4:58
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WarMap implements Component{

    private Integer x = 200 ,y = 50;
    private Integer width = 700, height = 700;
    private List<List<Plot>> plotList;
    // 地块边长
    private int plotSize;
    private int plotWidth = -1;
    private int plotHeight = -1;

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.ORANGE);
        g.drawRect(x-1, y-1, width+2, height+2);
        for(List<Plot> plotSubList: plotList) {
            for(Plot plot: plotSubList) {
                plot.paint(g);
            }
        }
    }

    public void initPlotList(List<List<Plot>> plotList) {
        this.plotList = plotList;
        plotWidth = plotList.get(0).size();
        plotHeight = plotList.size();
    }

    @Override
    public AreaPoint getAreaPoint(int areaSize) {
        return null;
    }

    @Override
    public void move(Game game) {

    }

    @Override
    public int getOrder() {
        return -100;
    }

    public Plot getPlot(int col, int row) {
        if(col >= 0 && col < plotWidth && row >= 0 && row < plotHeight) {
            return plotList.get(col).get(row);
        }else {
            return null;
        }
    }

    public Collection<Plot> getPlot(double ox, double oy, double r) {
        int oCol = (int)(ox - x)/plotSize;
        int oRow = (int)(oy - y)/plotSize;
        Set<Plot> result = new HashSet<>();
        Set<Plot> plots = new HashSet<>();
        plots.add(getPlot(oCol-1, oRow-1));
        plots.add(getPlot(oCol-1, oRow));
        plots.add(getPlot(oCol-1, oRow+1));
        plots.add(getPlot(oCol, oRow-1));
        plots.add(getPlot(oCol, oRow));
        plots.add(getPlot(oCol, oRow+1));
        plots.add(getPlot(oCol+1, oRow-1));
        plots.add(getPlot(oCol+1, oRow));
        plots.add(getPlot(oCol+1, oRow+1));
        plots.remove(null);
        for(Plot plot: plots) {
            boolean b = CrashUtil.ballSquare(ox, oy, r, plot.getX(), plot.getY(), plot.getSize());
            if(b) {
                result.add(plot);
            }
        }
        return result;
    }

}
